package com.hzw.saas.service.schedule.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.schedule.IUserScheduleTagService;
import com.hzw.saas.api.schedule.bo.UserScheduleTagBo;
import com.hzw.saas.api.schedule.query.UserScheduleTagQuery;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.schedule.constants.UserScheduleConst;
import com.hzw.saas.service.schedule.mapper.UserScheduleTagMapper;
import com.hzw.saas.service.schedule.model.UserScheduleTag;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author hankun
 */
@Service
public class UserScheduleTagServiceImpl extends ServiceImpl<UserScheduleTagMapper, UserScheduleTag> implements IService<UserScheduleTag>, IUserScheduleTagService {

    static {
        MapperUtil.nfFactory()
                .classMap(UserScheduleTag.class, UserScheduleTagBo.class)
                .field("pid", "tagId")
                .byDefault()
                .register();
    }

    @Resource
    private UserScheduleTagRelServiceImpl userScheduleTagRelService;

    @Override
    public List<UserScheduleTagBo> addTag(UserScheduleTagQuery userScheduleTagQuery) {
        String userId = userScheduleTagQuery.getUserId();
        UserScheduleTag userScheduleTag = MapperUtil.nf().map(userScheduleTagQuery, UserScheduleTag.class);
        userScheduleTag.setPid(SnowFlakeUtil.nextIdStr());
        userScheduleTag.setSortOrder(getNextSortOrder(userId));
        super.save(userScheduleTag);
        return listTag(userId);
    }

    @Override
    public List<UserScheduleTagBo> updateTag(UserScheduleTagQuery userScheduleTagQuery) {
        UserScheduleTag userScheduleTag = MapperUtil.nf().map(userScheduleTagQuery, UserScheduleTag.class);
        userScheduleTag.setPid(userScheduleTagQuery.getTagId());
        userScheduleTag.setEditorId(userScheduleTagQuery.getUserId());
        super.updateById(userScheduleTag);
        return listTag(userScheduleTagQuery.getUserId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserScheduleTagBo> deleteTag(UserScheduleTagQuery userScheduleTagQuery) {
        super.removeById(userScheduleTagQuery.getUserId());
        userScheduleTagRelService.deleteByTagIds(Collections.singletonList(userScheduleTagQuery.getTagId()));
        return listTag(userScheduleTagQuery.getUserId());
    }

    @Override
    public List<UserScheduleTagBo> listTag(String userId) {
        return this.baseMapper.listTag(userId, UserScheduleTag.DEFAULT_TAG);
    }

    private Long getNextSortOrder(String userId) {
        UserScheduleTag oneTag = super.lambdaQuery()
                .eq(UserScheduleTag::getUserId, userId)
                .orderByDesc(UserScheduleTag::getSortOrder)
                .last("limit 1")
                .one();
        return Objects.isNull(oneTag) ? 0 : oneTag.getSortOrder() + UserScheduleConst.INTERVAL_SORT;
    }
}
